package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.exception.MyException;
import com.example.pojo.Pub;
import com.example.pojo.Query;
import com.example.service.PubService;
import com.example.utils.Page;
import com.example.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin//允许跨域
@RequestMapping("/pub")
@RestController
@Api(tags = "酒店推荐")
public class PubController {
    @Autowired
    private PubService pubService;

    @GetMapping("/selectAllPub")
    public Result selectAllPub(Query query){//条件+分页查询
        Page<Pub> page=new Page<>(query.getPageNum(),query.getPageSize(),pubService.count(query.getName()));
        page.setList(pubService.selectAllPub(query.getName(),page.getStart(),query.getPageSize()));
        Map<String, Object> map = new HashMap<>();
        map.put("list", page.getList());
        map.put("count", page.getTotalCount());//总记录数
        map.put("pageSize", query.getPageSize());//每页记录数
        map.put("pageNum", query.getPageNum());//当前页码
        return new Result<>("200", "查询成功", map);
    }

    @GetMapping("/selectPubById")
    public Result selectPubById(Integer id) {//根据id查询
        Pub pub = pubService.selectPubById(id);
        Map<String,Object> map = new HashMap<>();
        if (pub != null) {
            map.put("code",200);
            map.put("data", pub);
            map.put("msg", "查询成功");
        }else {
            map.put("code",444);
            map.put("data", null);
            map.put("msg", "查询失败");
        }
        return new Result<>("222", "调用成功", map);
    }

    @GetMapping("/deletePub")
    public Result deletePub(Integer id) {//根据id删除
        int i = pubService.deletePub(id);
        Map<String, Object> map = new HashMap<>();
        if (i > 0) {
            map.put("code", 200);
            map.put("data", null);
            map.put("msg", "删除成功");
        } else {
            map.put("code", 444);
            map.put("data", null);
            map.put("msg", "删除失败");
        }
        return new Result<>("222", "调用成功", map);
    }

    @PostMapping("/updatePub")
    public Result updatePub(@RequestBody Pub pub) {//修改
        if (pubService.selectPubById(pub.getId()).getRecommend()==1){
            throw new MyException("444","该酒店已经推荐了");
        }
        if (pubService.count(new QueryWrapper<Pub>().eq("recommend",1))>7){
            throw new MyException("444","酒店推荐不能超过八个");
        }
        if (pubService.updatePub(pub) <= 0) {
            throw new MyException("444", "修改失败");
        }
        return new Result<>("200", "修改成功");
    }

    @PostMapping("/updatePub2")
    public Result updatePub2(@RequestBody Pub pub) {//修改
        if (pubService.selectPubById(pub.getId()).getRecommend()==0){
            throw new MyException("444","该酒店未推荐");
        }
        if (pubService.count(new QueryWrapper<Pub>().eq("recommend",1))<5){
            throw new MyException("444","酒店推荐不能小于四个");
        }
        if (pubService.updatePub2(pub) <= 0) {
            throw new MyException("444", "修改失败");
        }
        return new Result<>("200", "修改成功");
    }

}
